<?php

namespace app\common\model;

use think\Model;

/**
 * 空中课堂用户申请模型
*/
class OnlineClassroomUser extends Model
{
    // 表名
    protected $name = 'online_classroom_user';
    
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    
    // 追加属性
    protected $append = [
        'status_text',
        'join_time_text',
        'approve_time_text'
    ];

    /**
     * 状态文本获取器
     * @param string $value
     * @param array  $data
     * @return string
     */
    public function getStatusTextAttr($value, $data)
    {
        $status = [
            '0' => '待审核',
            '1' => '已通过',
            '2' => '已拒绝',
            '3' => '已取消'
        ];
        return isset($status[$data['status']]) ? $status[$data['status']] : '未知';
    }

    /**
     * 申请时间文本获取器
     * @param string $value
     * @param array  $data
     * @return string
     */
    public function getJoinTimeTextAttr($value, $data)
    {
        return $data['join_time'] ? date('Y-m-d H:i:s', $data['join_time']) : '';
    }

    /**
     * 审核时间文本获取器
     * @param string $value
     * @param array  $data
     * @return string
     */
    public function getApproveTimeTextAttr($value, $data)
    {
        return $data['approve_time'] ? date('Y-m-d H:i:s', $data['approve_time']) : '';
    }

    /**
     * 关联用户表
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id');
    }

    /**
     * 关联审核管理员
     * @return \think\model\relation\BelongsTo
     */
    public function approveAdmin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'approve_admin_id', 'id');
    }

    /**
     * 申请加入课堂
     * @param int    $user_id      用户ID
     * @param int    $classroom_id 课堂ID
     * @param string $apply_reason 申请理由
     * @return bool|mixed
     */
    public static function applyJoin($user_id, $classroom_id, $apply_reason = '')
    {
        // 检查是否已经申请过
        $exists = self::where([
            'user_id' => $user_id,
            'classroom_id' => $classroom_id
        ])->find();
        
        if ($exists) {
            return false; // 已经申请过
        }

        $data = [
            'user_id' => $user_id,
            'classroom_id' => $classroom_id,
            'join_time' => time(),
            'apply_reason' => $apply_reason,
            'status' => 0 // 待审核
        ];

        return self::create($data);
    }

    /**
     * 审核申请
     * @param int    $id           申请ID
     * @param int    $status       审核状态 1=已通过 2=已拒绝
     * @param int    $admin_id     管理员ID
     * @param string $reject_reason 拒绝理由
     * @return bool
     */
    public static function approve($id, $status, $admin_id, $reject_reason = '')
    {
        $application = self::find($id);
        if (!$application || $application->status != 0) {
            return false;
        }

        $data = [
            'status' => $status,
            'approve_admin_id' => $admin_id,
            'approve_time' => time()
        ];

        if ($status == 2 && $reject_reason) {
            $data['reject_reason'] = $reject_reason;
        }

        return $application->save($data);
    }

    /**
     * 取消申请
     * @param int $user_id      用户ID
     * @param int $classroom_id 课堂ID
     * @return bool
     */
    public static function cancelApply($user_id, $classroom_id)
    {
        $application = self::where([
            'user_id' => $user_id,
            'classroom_id' => $classroom_id,
            'status' => 0
        ])->find();

        if (!$application) {
            return false;
        }

        return $application->save(['status' => 3]); // 已取消
    }
}